package com.cluster.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.cluster.platform.common.api.CommonPage;
import com.cluster.platform.common.api.CommonResult;
import com.cluster.platform.dao.SysDictInfoDao;
import com.cluster.platform.dto.SysDictInfoDto;
import com.cluster.platform.mapper.SysDictInfoMapper;
import com.cluster.platform.model.SysDictInfo;
import com.cluster.platform.service.SysDictInfoService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName: SysDictInfoServiceImpl
 */
@Service
public class SysDictInfoServiceImpl implements SysDictInfoService {

    @Autowired
    SysDictInfoMapper sysDictInfoMapper;

    @Autowired
    SysDictInfoDao sysDictInfoDao;

    @Override
    @Transactional
    public CommonResult<Object> updateBatchDict(String status, List<String> ids) {
        if (StringUtils.isBlank(status) || ids.isEmpty()) {
            return CommonResult.failed("未获取参数信息");
        }
        int updateBatch = sysDictInfoMapper.updateBatchDict(status, ids);
        if (updateBatch<0) {
            return CommonResult.failed("操作失败");
        }
        return CommonResult.success(updateBatch);
    }

    @Override
    public CommonResult<Object> fetchList(SysDictInfoDto sysDictInfoDto) {
        if (null == sysDictInfoDto.getPageNum() || null == sysDictInfoDto.getPageSize()) {
            CommonResult.failed("未获取分页参数");
        }
        PageHelper.startPage(sysDictInfoDto.getPageNum(), sysDictInfoDto.getPageSize());
        SysDictInfo sysDictInfo = BeanUtil.copyProperties(sysDictInfoDto, SysDictInfo.class);
        List<SysDictInfo> sysDictInfos = sysDictInfoDao.searchSysDictInfo(sysDictInfo);
        if (sysDictInfos.isEmpty()) {
            CommonResult.failed("未查询到信息");
        }
        return CommonResult.success(CommonPage.restPage(sysDictInfos));
    }

    @Override
    public CommonResult<Object> searchSysDictInfo(SysDictInfo sysDictInfo) {
        List<SysDictInfo> sysDictInfos = sysDictInfoDao.searchSysDictInfo(sysDictInfo);
        if (sysDictInfos.isEmpty()) {
            CommonResult.failed("未查询到信息");
        }
        return CommonResult.success(sysDictInfos);
    }

    @Override
    @Transactional
    public CommonResult<Object> addSysDictInfo(SysDictInfo sysDictInfo) {
        if (BeanUtil.isEmpty(sysDictInfo)) {
            return CommonResult.failed("未获取参数信息");
        }
        sysDictInfo.setUseState("1");
        sysDictInfo.setIntDate(new Date());
        int insertSelective = sysDictInfoMapper.insertSelective(sysDictInfo);
        if (insertSelective < 0) {
            return CommonResult.failed("插入失败");
        }
        return CommonResult.success(sysDictInfo);
    }

    @Override
    @Transactional
    public CommonResult<Object> updateSysDictInfo(SysDictInfo sysDictInfo) {
        if (BeanUtil.isEmpty(sysDictInfo)) {
            return CommonResult.failed("未获取参数信息");
        }
        sysDictInfo.setUpdDate(new Date());
        int update = sysDictInfoMapper.updateByPrimaryKeySelective(sysDictInfo);
        if (update < 0) {
            return CommonResult.failed("更新失败");
        }
        return CommonResult.success(sysDictInfo);
    }

    @Override
    @Transactional
    public CommonResult<Object> delSysDictInfo(SysDictInfo sysDictInfo) {
        if (BeanUtil.isEmpty(sysDictInfo)) {
            return CommonResult.failed("未获取参数信息");
        }
        sysDictInfo.setUpdDate(new Date());
        sysDictInfo.setUseState("0");
        int update = sysDictInfoMapper.updateByPrimaryKeySelective(sysDictInfo);
        if (update < 0) {
            return CommonResult.failed("删除失败");
        }
        return CommonResult.success(sysDictInfo);
    }
}
